package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.Class;
import com.sims.dao.IClassDAO;

/**
 * A data access object (DAO) providing persistence and search support for Class
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.sims.bean.Class
 * @author MyEclipse Persistence Tools
 */

public class ClassDAO extends HibernateDaoSupport implements IClassDAO {
	private static final Logger log = LoggerFactory.getLogger(ClassDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String GRADE = "grade";
	public static final String CLASSROOM = "classroom";
	public static final String TID = "tid";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#save(com.sims.bean.Class)
	 */
	@Override
	public void save(Class transientInstance) {
		log.debug("saving Class instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#delete(com.sims.bean.Class)
	 */
	@Override
	public void delete(Class persistentInstance) {
		log.debug("deleting Class instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findById(java.lang.Integer)
	 */
	@Override
	public Class findById(java.lang.Integer id) {
		log.debug("getting Class instance with id: " + id);
		try {
			Class instance = (Class) getHibernateTemplate().get(
					"com.sims.bean.Class", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByExample(com.sims.bean.Class)
	 */
	@Override
	public List findByExample(Class instance) {
		log.debug("finding Class instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Class instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Class as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByName(java.lang.Object)
	 */
	@Override
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByGrade(java.lang.Object)
	 */
	@Override
	public List findByGrade(Object grade) {
		return findByProperty(GRADE, grade);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByClassroom(java.lang.Object)
	 */
	@Override
	public List findByClassroom(Object classroom) {
		return findByProperty(CLASSROOM, classroom);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findByTid(java.lang.Object)
	 */
	@Override
	public List findByTid(Object tid) {
		return findByProperty(TID, tid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Class instances");
		try {
			String queryString = "from Class";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#merge(com.sims.bean.Class)
	 */
	@Override
	public Class merge(Class detachedInstance) {
		log.debug("merging Class instance");
		try {
			Class result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#attachDirty(com.sims.bean.Class)
	 */
	@Override
	public void attachDirty(Class instance) {
		log.debug("attaching dirty Class instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IClassDAO#attachClean(com.sims.bean.Class)
	 */
	@Override
	public void attachClean(Class instance) {
		log.debug("attaching clean Class instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IClassDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IClassDAO) ctx.getBean("ClassDAO");
	}
}